package com.jackrain.nea.fc.ps.mapper;


import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Mapper
public interface OmsProDistribMapper {

    //查询所有 配销中心的商品档案
    @Select("SELECT " +
            "ID,ENAME,PS_C_BRAND_ID ,ECODE ,PRICELIST " +
            "FROM PS_C_PRO " +
            "WHERE CP_C_DISTRIB_ID=1252 AND ISACTIVE='Y' ")
    List<HashMap> selectProdistrib();

    @Select("SELECT ENAME FROM PS_C_BRAND WHERE ID=#{brandid}")
    String selectBrandName(@Param("brandid") long brandid);


    //根据商品的id获取对应条码信息
    @Select("SELECT " +
            "ECODE, " +
            "GBCODE, " +
            "PS_C_SPEC1OBJ_ID, " +
            "PS_C_SPEC2OBJ_ID " +
            "FROM PS_C_SKU " +
            "WHERE PS_C_PRO_ID=#{proid} ")
    List<HashMap> selectSkudistrib(@Param("proid") long proid);

    //根据规格ID获取 规格的编码和名字
    @Select("SELECT ECODE,ENAME FROM PS_C_SPECOBJ WHERE ID=#{id}")
    HashMap selectPsCSpecobj(@Param("id") long id);

    @SelectProvider(type = ViewPsProSql.class, method = "selectProdistribById")
    List<HashMap> selectProdistribById(@Param("id") Object[] id);

    class ViewPsProSql {
        public String selectProdistribById(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            return new SQL() {
                {
                    SELECT("ID, "+
                                    "ENAME , " +
                                    "PS_C_BRAND_ID , " +
                                    "ECODE , " +
                                    "PRICELIST ");
                    FROM("PS_C_PRO");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                    WHERE("CP_C_DISTRIB_ID=1252");
                }
            }.toString();
        }
    }


}
